/***************************************************************************
 * 文件名：AuxiliaryToolUtil
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/12 9:14:53
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.UIAutomation;
using DevExpress.Utils;
using System;
using System.Collections.Generic;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Util;

namespace AuxiliaryTools
{
	public class AuxiliaryToolUtil
	{
		#region 工具配置操作

		/// <summary>
		/// 获取工具配置
		/// </summary>
		/// <param name="filePath"></param>
		/// <returns></returns>
		public static List<ToolModel> GetToolConfiguration(string filePath)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
			{
				return null;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return null;
			}

			XmlNode singleNode = document.SelectSingleNode("//Tools");
			if (singleNode == null || singleNode.ChildNodes?.Count == 0)
			{
				return null;
			}

			List<ToolModel> tools = new List<ToolModel>();

			foreach (XmlNode node in singleNode.ChildNodes)
			{
				if (node == null) { continue; }

				string name = node.Attributes["Name"]?.Value;
				string toolFileDir = node.Attributes["ToolFileDir"]?.Value;
				string programEntrance = node.Attributes["ProgramEntrance"]?.Value;
				string imagePath = node.Attributes["ImagePath"]?.Value;
				string startMode = node.Attributes["StartMode"]?.Value;
				string isLocalApplication = node.Attributes["IsLocalApplication"]?.Value;
				string isFixed = node.Attributes["IsFixed"]?.Value;
				string isRecentlyUsed = node.Attributes["IsRecentlyUsed"]?.Value;
				string sort = node.Attributes["Sort"]?.Value;

				if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(programEntrance))
				{
					continue;
				}

				//添加至返回列表
				tools.Add(new ToolModel
				{
					Name = name,
					ToolFileDir = toolFileDir,
					ProgramEntrance = programEntrance,
					ImagePath = imagePath,
					StartMode = startMode,
					IsLocalApplication = !string.IsNullOrEmpty(isLocalApplication) && bool.Parse(isLocalApplication),
					IsFixed = !string.IsNullOrEmpty(isFixed) && bool.Parse(isFixed),
					IsRecentlyUsed = !string.IsNullOrEmpty(isRecentlyUsed) && bool.Parse(isRecentlyUsed),
					Sort = !string.IsNullOrEmpty(sort) ? int.Parse(sort) : 0
				});
			}

			return tools;
		}

		/// <summary>
		/// 获取
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="nodeName"></param>
		/// <returns></returns>
		public static ToolModel GetSigleNodeToolConfiguration(string filePath,string nodeName)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
			{
				return null;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return null;
			}

			XmlNode singleNode = document.SelectSingleNode("//Tools");
			if (singleNode == null || singleNode.ChildNodes?.Count == 0)
			{
				return null;
			}

			foreach (XmlNode node in singleNode.ChildNodes)
			{
				if (node == null) { continue; }

				string name = node.Attributes["Name"]?.Value;

				if (!name.Equals(nodeName))
				{
					continue;
				}

				string toolFileDir = node.Attributes["ToolFileDir"]?.Value;
				string programEntrance = node.Attributes["ProgramEntrance"]?.Value;
				string imagePath = node.Attributes["ImagePath"]?.Value;
				string startMode = node.Attributes["StartMode"]?.Value;
				string isLocalApplication = node.Attributes["IsLocalApplication"]?.Value;
				string isFixed = node.Attributes["IsFixed"]?.Value;
				string isRecentlyUsed = node.Attributes["IsRecentlyUsed"]?.Value;
				string sort = node.Attributes["Sort"]?.Value;

				if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(programEntrance))
				{
					continue;
				}

			    return new ToolModel
				{
					Name = name,
					ToolFileDir = toolFileDir,
					ProgramEntrance = programEntrance,
					ImagePath = imagePath,
					StartMode = startMode,
					IsLocalApplication = !string.IsNullOrEmpty(isLocalApplication) && bool.Parse(isLocalApplication),
					IsFixed = !string.IsNullOrEmpty(isFixed) && bool.Parse(isFixed),
					IsRecentlyUsed = !string.IsNullOrEmpty(isRecentlyUsed) && bool.Parse(isRecentlyUsed),
					Sort = !string.IsNullOrEmpty(sort) ? int.Parse(sort) : 0
				};
			}

			return null;
		}

		/// <summary>
		/// 更新工具配置项节点属性值
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="nodeName"></param>
		/// <param name="valuePairs"></param>
		public static bool UpdateToolConfiguration(string filePath, string nodeName, Dictionary<string, string> valuePairs)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
			{
				return false;
			}

			if (valuePairs?.Count == 0)
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}

			XmlNode singleNode = document.SelectSingleNode("//Tools");
			if (singleNode == null || singleNode.ChildNodes?.Count == 0)
			{
				return false;
			}

			foreach (XmlNode node in singleNode.ChildNodes)
			{
				string name = node.Attributes["Name"]?.Value;
				if (string.IsNullOrEmpty(name) || !name.Equals(nodeName))
				{
					continue;
				}

				//更新节点属性
				foreach (var valuePair in valuePairs)
				{
					if (string.IsNullOrEmpty(valuePair.Key) || string.IsNullOrEmpty(valuePair.Value))
					{
						continue;
					}
					node.Attributes[valuePair.Key].Value = valuePair.Value;
				}

			}
			document.Save(filePath);
			return true;
		}

		/// <summary>
		/// 添加工具配置
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="tool"></param>
		/// <returns></returns>
		public static bool AddToolConfiguration(string filePath, ToolModel tool)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || tool == null)
			{
				return false;
			}
			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}
			XmlNode singleNode = document.SelectSingleNode("//Tools");
			if (singleNode == null)
			{
				return false;
			}
			//添加工具配置
			XmlElement setting = document.CreateElement("Tool");
			setting.SetAttribute("Name", tool.Name);
			setting.SetAttribute("ToolFileDir",tool.ToolFileDir);
			setting.SetAttribute("ProgramEntrance", tool.ProgramEntrance);
			setting.SetAttribute("ImagePath", tool.ImagePath);
			setting.SetAttribute("StartMode", tool.StartMode.ToString());
			setting.SetAttribute("IsLocalApplication",tool.IsLocalApplication.ToString());
			setting.SetAttribute("IsFixed", tool.IsFixed.ToString());
			setting.SetAttribute("IsRecentlyUsed", tool.IsRecentlyUsed.ToString());
			setting.SetAttribute("Sort", tool.Sort.ToString());
			singleNode.AppendChild(setting);
			document.Save(filePath);
			return true;
		}

		/// <summary>
		/// 批量添加工具配置
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="tools"></param>
		/// <returns></returns>
		public static bool AddToolConfigurations(string filePath,List<ToolModel> tools)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || tools?.Count == 0)
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}
			XmlNode singleNode = document.SelectSingleNode("//Tools");
			if (singleNode == null)
			{
				return false;
			}

			foreach(var tool in tools)
			{
				//添加工具配置
				XmlElement setting = document.CreateElement("Tool");
				setting.SetAttribute("Name", tool.Name);
				setting.SetAttribute("ToolFileDir", tool.ToolFileDir);
				setting.SetAttribute("ProgramEntrance", tool.ProgramEntrance);
				setting.SetAttribute("ImagePath", tool.ImagePath);
				setting.SetAttribute("StartMode", tool.StartMode.ToString());
				setting.SetAttribute("IsLocalApplication", tool.IsLocalApplication.ToString());
				setting.SetAttribute("IsFixed", tool.IsFixed.ToString());
				setting.SetAttribute("IsRecentlyUsed", tool.IsRecentlyUsed.ToString());
				setting.SetAttribute("Sort", tool.Sort.ToString());
				singleNode.AppendChild(setting);
			}

			document.Save(filePath);
			return true;
		}

		/// <summary>
		/// 删除工具项配置
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static bool DeleteToolConfiguration(string filePath, string name)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || string.IsNullOrEmpty(name))
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}

			XmlNode singleNode = document.SelectSingleNode("//Tools");

			if (singleNode?.ChildNodes.Count == 0)
			{
				return false;
			}

			foreach (XmlNode node in singleNode.ChildNodes)
			{
				if (node == null) { continue; }
				string connectName = node.Attributes["Name"].Value;
				if (string.IsNullOrEmpty(connectName) || !connectName.Equals(name)) { continue; }

				//删除工具项配置
				singleNode.RemoveChild(node);
				break;
			}

			//保存文件
			document.Save(filePath);

			return true;
		}

		#endregion

	}
}
